package com.inxedu.os.edu.service.user.impl;

import com.inxedu.os.edu.dao.SysLoginLogMapper;
import com.inxedu.os.edu.dao.SysRoleMapper;
import com.inxedu.os.edu.dao.SysUserMapper;
import com.inxedu.os.edu.pojo.*;
import com.inxedu.os.edu.service.user.AdminSysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class AdminSysUserImpl implements AdminSysUser {
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysLoginLogMapper sysLoginLogMapper;
    @Override
    // 修改查询
    public SysUser selectAll(Integer userId) {
        SysUser sysUser = sysUserMapper.selectByPrimaryKey(userId);
        return sysUser;
    }
    //修改
    @Override
    public void updatauser(SysUser sysUser) {
        System.out.println(sysUser);
        int i = sysUserMapper.updateByPrimaryKeySelective(sysUser);
    }
    //查询身份
    @Override
    public List<SysRole> selectRole() {
        SysRoleExample sysRoleExample=new SysRoleExample();
        List<SysRole> sysRoles = sysRoleMapper.selectByExample(sysRoleExample);
        return sysRoles;
    }

    //模糊查询
    @Override
    public List<SysUser> select(SysUser sysUser) {
        SysUserExample example=new SysUserExample();
        SysUserExample.Criteria criteria = example.createCriteria();
        criteria.andLoginNameLike("%"+sysUser.getLoginName()+"%");
        criteria.andLoginNameLike("%"+sysUser.getEmail()+"%");
        List<SysUser> sysUsers = sysUserMapper.selectByExample(example);
        List res = new ArrayList();
        for (SysUser user : sysUsers) {
            if(user.getStatus()!=2){
                res.add(user);
            }
        }
        return res;
    }
    //日志
    @Override
    public List<SysLoginLog> selectByExample(Integer userId) {
        SysLoginLogExample sysLoginLogExample=new SysLoginLogExample();
        SysLoginLogExample.Criteria criteria = sysLoginLogExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        List<SysLoginLog> sysLoginLogs = sysLoginLogMapper.selectByExample(sysLoginLogExample);
        return sysLoginLogs;
    }
    //伪删除
    @Override
    public void updatestatus(Integer status, int type) {
        SysUser user=new SysUser();
        user.setUserId(status);
        user.setStatus(type);
         sysUserMapper.updateByPrimaryKeySelective(user);
    }
    //修改密码
    @Override
    public void updadauserpwd(int userId, String password) {
        SysUser sysUser=new SysUser();
        sysUser.setUserId(userId);
        sysUser.setLoginPwd(password);
        int i = sysUserMapper.updateByPrimaryKeySelective(sysUser);
        System.out.println(i);
    }

    //新增
    @Override
    public int insetsysuser(SysUser sysUser) {
        Date date=new Date();
        sysUser.setCreateTime(date);

        int i = sysUserMapper.insertSelective(sysUser);
        return i;
    }
    @Override
    public void deleByID(Integer id) {

        sysUserMapper.deleteByPrimaryKey(id);
    }

}
